package lynn.java8;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * Unit test for simple App.
 */
public class AppTestDateTimeDemo {

    @Test
    public void DateTimeDemo() {

        LocalDate curDate = LocalDate.now();
        System.out.println(curDate);//2017-09-18

        LocalTime curTime = LocalTime.now();
        System.out.println(curTime);//14:29:11.014

        LocalDateTime curDateTime = LocalDateTime.now();
        System.out.println(curDateTime);//2017-09-18T14:30:34.802
        System.out.println(curDateTime.toLocalDate());
        System.out.println(curDateTime.toLocalTime());

    }

    @Test
    public void DateTimeDemo2() {

        LocalDate curDate = LocalDate.now();
        System.out.println(curDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));
        System.out.println(curDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)));
        System.out.println(curDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)));
        System.out.println(curDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));


        LocalTime curTime = LocalTime.now();
        System.out.println(curTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG)));
        System.out.println(curTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)));
        System.out.println(curTime.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)));


        LocalDateTime curDateTime = LocalDateTime.now();
        System.out.println(curDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG)));
        System.out.println(curDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
        System.out.println(curDateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));

        System.out.println(curDateTime.format(DateTimeFormatter.ofPattern("MM d',' yyyy H':'mm a")));
        System.out.println(curDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        System.out.println(curDateTime.getDayOfYear());
        System.out.println(curDateTime.getDayOfWeek().getValue());
        System.out.println(curDateTime.getDayOfMonth());
        System.out.println(curDateTime.getHour());
        System.out.println(curDateTime.getMonth());
        System.out.println(curDateTime.getMonthValue());
        System.out.println(curDateTime.getNano());

    }

    @Test
    public void DateTimeDemo3() {

        LocalDateTime curDateTime = LocalDateTime.now();

        System.out.println(curDateTime.getDayOfYear());
        System.out.println(curDateTime.getDayOfWeek().getValue());
        System.out.println(curDateTime.getDayOfMonth());
        System.out.println(curDateTime.getHour());
        System.out.println(curDateTime.getMonth().maxLength());
        System.out.println(curDateTime.getMonthValue());
        System.out.println(curDateTime.getNano());

    }

    @Test
    public void DateTimeDemo4() {

        LocalDateTime dateTime = LocalDateTime.parse("2017-09-18 14:56:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(dateTime);

    }

    @Test
    public void InstantDemo() {

        Instant instant = Instant.now();
        System.out.println(instant.getEpochSecond());
        System.out.println(System.currentTimeMillis());
        System.out.println(instant);

        System.out.println(instant.toEpochMilli());
    }

    @Test
    public void PeriodDemo(){

        LocalDate curDate1 = LocalDate.now();
        System.out.println(curDate1);

        LocalDate curDate2 = LocalDate.parse("2017-09-20 14:56:11", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));;
        System.out.println(curDate2);

        Period period = Period.between(curDate1,curDate2);
        System.out.println(period.getDays());
        System.out.println(period.getYears());

    }

    @Test
    public void DurationDemo(){
        LocalTime time1 = LocalTime.now();
        LocalTime time2 = time1.plus(Duration.ofHours(2));

        Duration duration = Duration.between(time1, time2);
        System.out.println("Duration: " + duration.get(ChronoUnit.SECONDS));
    }



    @Test
    public void timestampToDateTimeString() {
        String formatString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(new Long(1505685600000L)));
        System.out.println(1505685600000L);
        System.out.println(formatString);
    }

    @Test
    public void DateTimeStringToTimeStamp() throws ParseException {
        Long time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-09-18 06:00:00").getTime();
        System.out.println(time);
    }

    @Test
    public void ChronoUnitsDemo(){
        //Get the current date
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);
        //add 1 week to the current date
        LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("Next week: " + nextWeek);
        //add 1 month to the current date
        LocalDate nextMonth = today.plus(1, ChronoUnit.MONTHS);
        System.out.println("Next month: " + nextMonth);
        //add 1 year to the current date
        LocalDate nextYear = today.plus(1, ChronoUnit.YEARS);
        System.out.println("Next year: " + nextYear);
        //add 10 years to the current date
        LocalDate nextDecade = today.plus(1, ChronoUnit.DECADES);
        System.out.println("Date after ten year: " + nextDecade);
    }


    @Test
    public void ClockDemo() {

        Clock clock = Clock.systemUTC();
        System.out.println(clock.millis());
        System.out.println(clock.instant());

    }


}
